package com.bigdata.mapreduce.topn;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Tkey implements WritableComparable<Tkey> {
    private int year;
    private int month;
    private int day;
    private int temp;

    private String cityName;

    /**
     * 重写排序比较器
     * 按照年月日正序排列
     *
     * @return
     */
    @Override
    public int compareTo(Tkey o) {
        // 按照年月日正序排列
        int cYear = Integer.compare(year, o.year);
        if (cYear == 0) {// 年份相同
            // 比较月
            int cMonth = Integer.compare(month, o.month);
            if (cMonth == 0) {
                // 比较日的结果直接返回
                return Integer.compare(day, o.day);
            }
            return cMonth;
        }
        return cYear;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(year);
        out.writeInt(month);
        out.writeInt(day);
        out.writeInt(temp);
        out.writeUTF(cityName);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt();
        this.month = in.readInt();
        this.day = in.readInt();
        this.temp = in.readInt();
        this.cityName = in.readUTF();
    }


    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    @Override
    public String toString() {
        return "Tkey{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", temp=" + temp +
                ", cityName='" + cityName + '\'' +
                '}';
    }
}
